package com.test.crawler.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 商品爬虫实现
 */
@Slf4j
public class MallProductDownloader extends ChromeDriverDownloader<MallProduct> {

    @Override
    public MallProduct craw(WebDriver webDriver, String url) {
        webDriver.get(url);
        System.out.println("########################okkkk====================");
        MallProduct productInfo = new MallProduct();
        productInfo.extractBy().forEach((productProperty, extractBy) -> {
            Object propertyValue = getExtractByValue(webDriver, extractBy);
            setValue(productInfo, productProperty, propertyValue);
        });
        String skuSelector = "#spec_box > div.dtl-inf-l.grey888";

        List<MallSku> skuInfos = new ArrayList<>();

        String skuGroupSelector = "#text_specBox";//多规格分组ID

        //遍历sku销售属性，比如颜色分类、网络类型、存储容量、套餐类型等
        try {
            skuSalePropClick(webDriver, skuInfos, webDriver.findElements(By.cssSelector(skuSelector)), 0, "", "");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        productInfo.setSkuInfos(skuInfos);
        return productInfo;
    }


    private void skuSalePropClick(WebDriver webDriver, List<MallSku> skuInfos, List<WebElement> skuSaleProps, int index, String parentSkuKey, String groupKey) throws Exception {
        String skuSelector = "#spec_box";
        //System.out.println(index + "->" + skuSaleProps.size());
        if (skuSaleProps.size() == 0) {
            return;
        }
        String skuSalePropName = skuSaleProps.get(index).getText();
        // System.out.println("选择sku属性项："+skuSalePropName);
        //遍历某个sku销售属性，比如颜色分类下的金色、银色、灰色等
        List<WebElement> skuSalePropValues = webDriver.findElements(By.cssSelector(skuSelector+">div> ul > li"));
        skuSalePropValues.removeIf(webElement -> !webElement.isDisplayed());
        // System.out.println("当前规格数量："+skuSalePropValues.size());
        for (int i = 0; i < skuSalePropValues.size(); i++) {
            if (index == 0) parentSkuKey = groupKey;
            WebElement skuSalePropValue = skuSalePropValues.get(i);
            if (!Objects.equals(skuSalePropValue.getAttribute("class"), "dtl-inf-rur")) {//当前元素已经被选中则无需点击
                skuSalePropValue.click();//选中当前规格
                Thread.sleep(2000);
            }
            skuSaleProps=webDriver.findElements(By.cssSelector(skuSelector));
            List<WebElement> newSkuSalePropValues = skuSaleProps.get(index).findElements(By.cssSelector("div > ul > li>a"));
            newSkuSalePropValues.removeIf(webElement -> !webElement.isDisplayed());
            skuSalePropValue = newSkuSalePropValues.get(i);
            String skuSalePropValueName = skuSalePropValue.getText();//未选中的规格名
            if (StringUtils.isEmpty(skuSalePropValueName)) {
                skuSalePropValue = skuSalePropValue.findElement(By.cssSelector("a"));
                skuSalePropValueName = skuSalePropValue.getText();//选中的规格名
            }
            System.out.println("选中规格：" + skuSalePropValueName);

           Thread.sleep(1000);
            if (index < skuSaleProps.size() - 1) {
             System.out.println("当前规格："+skuSalePropValueName);
                skuSalePropClick(webDriver, skuInfos, webDriver.findElements(By.cssSelector(skuSelector)), index + 1, skuSalePropValueName, groupKey);
            } else {
                System.out.println("当前规格：" + parentSkuKey + skuSalePropName + ":" + skuSalePropValueName + ";");//颜色分类 -> 金色
                MallSku skuInfo = new MallSku();
                //skuInfo.setKeys(parentSkuKey +  ":" + skuSalePropName + ";");
                skuInfo.setKeys(parentSkuKey + skuSalePropName+ ":"+skuSalePropValueName + ";");
                skuInfo.extractBy().forEach((skuProperty, extractBy) -> {
                    Object propertyValue = getExtractByValue(webDriver, extractBy);
//          String propertyValue = webDriver.findElement(By.cssSelector(extractBy.value())).getText();
                   System.out.println(skuProperty + ":" + propertyValue);
                    setValue(skuInfo, skuProperty, propertyValue);
                });
                skuInfos.add(skuInfo);
            }
        }
    }
}
